//package me.shiqiang.im.dao.elastic;
//
//import com.fasterxml.jackson.databind.JsonNode;
//import me.shiqiang.im.msg.SndMsg;
//import me.shiqiang.im.utils.JsonUtils;
//import org.apache.http.HttpEntity;
//import org.apache.http.entity.ContentType;
//import org.apache.http.nio.entity.NStringEntity;
//import org.apache.http.util.EntityUtils;
//import org.elasticsearch.client.Response;
//import org.elasticsearch.client.RestClient;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//
///**
// * @author cuishiqiang
// * @date 2018/8/21 下午2:21
// */
//@Component
//public class MessageDao {
//
//    @Autowired
//    RestClient client;
//
//    public List<SndMsg> searchMsgs(long sessionId,boolean asc,int pageNum,int pageSize) throws IOException {
//        HttpEntity entity = new NStringEntity(
//                "{\n" +
//                "  \"from\": "+((pageNum-1) * pageSize)+",\n" +
//                "  \"size\":"+pageSize+",\n" +
//                "  \"_source\": [\n" +
//                "    \"time\",\n" +
//                "    \"from\",\n" +
//                "    \"fromName\",\n" +
//                "    \"msg\",\n" +
//                "    \"cType\",\n" +
//                "    \"mType\",\n" +
//                "    \"to\"\n" +
//                "  ], \n" +
//                "  \"query\":{\n" +
//                "    \"match\": {\n" +
//                "      \"to\":"+sessionId+"\n" +
//                "    }\n" +
//                "  },\n" +
//                "  \"sort\": [\n" +
//                "    {\n" +
//                "      \"time\": {\n" +
//                "        \"order\": \""+(asc?"asc":"desc")+"\"\n" +
//                "      }\n" +
//                "    }\n" +
//                "  ]\n" +
//                "}", ContentType.APPLICATION_JSON);
//        Response response = client.performRequest(
//                "GET",
//                "/filebeat*/_search",
//                Collections.emptyMap(),
//                entity);
//        String resultJson = EntityUtils.toString(response.getEntity());
//        JsonNode jsonNode = JsonUtils.getInstance().getMapper().readTree(resultJson);
//        JsonNode hits = jsonNode.path("hits").path("hits");
//        List<SndMsg> msgs = new ArrayList<>();
//        if (hits.isArray()) {
//            for (JsonNode node : hits) {
//                JsonNode msg = node.get("_source");
//                SndMsg sndMsg = JsonUtils.getInstance().fromJson(msg.toString(), SndMsg.class);
//                msgs.add(sndMsg);
//            }
//        }
//        return msgs;
//    }
//
//}
